package com.loan.service;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import com.jfinal.plugin.activerecord.Page;
import com.loan.model.Logger;
import com.loan.model.Staff;
import com.loan.model.User;
import com.loan.util.Str;

public class LoggerService extends BaseService<Logger>{
	
	private StaffService staff = new StaffService();
	
	/**
	 * 新增日志记录
	 */
	public void build(String name, String contents, int userId, String type) {
		Logger log = new Logger(); 
		log.setName(name);
		log.setContents(contents); 
		if(userId>0){
			User u = User.dao.findById(userId);
			log.setUserId(userId);
			log.setRealName(u.getRealName());
			log.setMobile(u.getMobile());
			log.setType("user");
		}else{
			log.setUserId(0);
			log.setType("admin");
		}
		if(Str.nb(type)){
			log.setType(type);
			if(Str.eq("admin", type)) {
				Staff s = staff.findByUserId(userId);
				log.setRole(s!=null ? s.getRole() : "none");
			}
		}
		log.setCreateTime(new Date());
		log.save();
	}
	
	/**
	 * 分页数据
	 */ 
	public Page<Logger> list(int page, int size, Map<String, String> map) {
		return Logger.me.paginate(page, size, buildSql("logger",Arrays.asList("real_name", "name", "role", "user_id", "mobile", "create_time", "type"),map));
	}
}
